const http = require('http');
const  app  = require('../app')
const server = http.createServer(app)
const WebSocket = require('ws');
console.log(app);



const wss = new WebSocket.Server({ noServer: true });

// 处理 WebSocket 连接
wss.on('connection', function connection(ws) {
    ws.on('message', function incoming(message) {
        console.log('received: %s', message);
    });

    ws.send('Hello! Message From Server!!');
});

// HTTP 服务器上的 '/websocket' 路由
app.get('/websocket', (req, res) => {
    res.send('WebSocket route');
});

// 监听 'upgrade' 事件以进行 WebSocket 握手
server.on('upgrade', function upgrade(request, socket, head) {
    // 检查请求的路径
    if (request.url === '/websocket') {
        wss.handleUpgrade(request, socket, head, function done(ws) {
            wss.emit('connection', ws, request);
        });
    } else {
        socket.destroy();
    }
});